package com.shiku.imserver.common.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Node;
import org.tio.core.Tio;
import org.tio.http.common.Cookie;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.HttpConst;
import org.tio.http.common.RequestLine;
import org.tio.http.common.UploadFile;
import org.tio.http.common.session.HttpSession;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.StrUtil;

public class HttpRequest extends HttpPacket {
    private static Logger log = LoggerFactory.getLogger(HttpRequest.class);
    private static final long serialVersionUID = -3849253977016967211L;
    private HttpConst.RequestBodyFormat bodyFormat;
    private String bodyString;
    public ChannelContext channelContext;
    private String charset = "utf-8";
    private String clientIp = null;
    private boolean closed = false;
    private String connection;
    private int contentLength;
    private Map<String, Cookie> cookieMap = null;
    private List<Cookie> cookies = null;
    private long createTime = SystemTimer.currTime;
    private String domain = null;
    private Integer forwardCount = null;
    protected Map<String, String> headers = new HashMap();
    private String host = null;
    public HttpConfig httpConfig;
    private HttpSession httpSession;
    private Boolean isAjax = null;
    private boolean isForward = false;
    private Boolean isSupportGzip = null;
    private boolean needForward = false;
    private Map<String, Object[]> params = new HashMap();
    private Node remote = null;
    public RequestLine requestLine = null;

    public HttpRequest(Node remote2) {
        this.remote = remote2;
    }

    public HttpRequest() {
    }

    public void close() {
        close(null);
    }

    public void close(String remark) {
        this.closed = true;
        Tio.remove(this.channelContext, remark);
    }

    public void addParam(String key, Object value) {
        if (value != null) {
            Object[] existValue = this.params.get(key);
            if (existValue != null) {
                Object[] newExistValue = new Object[(existValue.length + 1)];
                if (value instanceof String) {
                    newExistValue = new String[(existValue.length + 1)];
                } else if (value instanceof UploadFile) {
                    newExistValue = new UploadFile[(existValue.length + 1)];
                }
                System.arraycopy(existValue, 0, newExistValue, 0, existValue.length);
                newExistValue[newExistValue.length - 1] = value;
                this.params.put(key, newExistValue);
                return;
            }
            Object[] newExistValue2 = null;
            if (value instanceof String) {
                newExistValue2 = new String[]{(String) value};
            } else if (value instanceof UploadFile) {
                newExistValue2 = new UploadFile[]{(UploadFile) value};
            }
            this.params.put(key, newExistValue2);
        }
    }

    public HttpResponse forward(String newPath) throws Exception {
        if (StrUtil.contains(newPath, '?')) {
            this.requestLine.path = StrUtil.subBefore(newPath, "?", false);
            this.requestLine.queryString = StrUtil.subAfter(newPath, "?", false);
        } else {
            this.requestLine.path = newPath;
            this.requestLine.queryString = null;
        }
        if (this.forwardCount == null) {
            this.forwardCount = 1;
        } else {
            Integer num = this.forwardCount;
            this.forwardCount = Integer.valueOf(this.forwardCount.intValue() + 1);
        }
        if (this.forwardCount.intValue() > this.httpConfig.maxForwardCount) {
            log.error("forwardCount[{}] is too large, newPath:{}", this.forwardCount, newPath);
            close();
            return null;
        }
        this.needForward = true;
        return HttpResponse.NULL_RESPONSE;
    }

    public HttpConst.RequestBodyFormat getBodyFormat() {
        return this.bodyFormat;
    }

    public String getUserAgent() {
        return this.headers.get("user-agent");
    }

    public String getHost() {
        if (this.host != null) {
            return this.host;
        }
        this.host = this.headers.get("host");
        return this.host;
    }

    public String getClientIp() {
        return this.clientIp;
    }

    public void addHeader(String key, String value) {
        this.headers.put(key, value);
    }

    public void addHeaders(Map<String, String> headers2) {
        if (headers2 != null) {
            for (Map.Entry<String, String> entry : headers2.entrySet()) {
                addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    public String getDomain() {
        if (this.domain != null) {
            return this.domain;
        }
        if (StrUtil.isBlank(getHost())) {
            return null;
        }
        this.domain = StrUtil.subBefore(getHost(), ":", false);
        return this.domain;
    }

    public String getBodyString() {
        return this.bodyString;
    }

    public ChannelContext getChannelContext() {
        return this.channelContext;
    }

    public String getCharset() {
        return this.charset;
    }

    public int getContentLength() {
        return this.contentLength;
    }

    public Cookie getCookie(String cooiename) {
        if (this.cookieMap == null) {
            return null;
        }
        return this.cookieMap.get(cooiename);
    }

    public Map<String, Cookie> getCookieMap() {
        return this.cookieMap;
    }

    public List<Cookie> getCookies() {
        return this.cookies;
    }

    public HttpConfig getHttpConfig() {
        return this.httpConfig;
    }

    public HttpSession getHttpSession() {
        return this.httpSession;
    }

    public String getHeader(String key) {
        return this.headers.get(key);
    }

    public Map<String, String> getHeaders() {
        return this.headers;
    }

    public void removeHeader(String key, String value) {
        this.headers.remove(key);
    }

    public Boolean getIsAjax() {
        if (this.isAjax == null) {
            String X_Requested_With = getHeader("x-requested-with");
            if (X_Requested_With == null || !"XMLHttpRequest".equalsIgnoreCase(X_Requested_With)) {
                this.isAjax = false;
            } else {
                this.isAjax = true;
            }
        }
        return this.isAjax;
    }

    public Boolean getIsSupportGzip() {
        return true;
    }

    public Map<String, Object[]> getParams() {
        return this.params;
    }

    public Object getObject(String name) {
        Object[] values = this.params.get(name);
        if (values == null || values.length <= 0) {
            return null;
        }
        return values[0];
    }

    public String getParam(String name) {
        Object[] values = this.params.get(name);
        if (values == null || values.length <= 0) {
            return null;
        }
        return (String) values[0];
    }

    public String getString(String name) {
        return getParam(name);
    }

    public UploadFile getUploadFile(String name) {
        Object[] values = this.params.get(name);
        if (values == null || values.length <= 0) {
            return null;
        }
        return (UploadFile) values[0];
    }

    public Integer getInt(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Integer.valueOf(Integer.parseInt(value));
    }

    public Short getShort(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Short.valueOf(Short.parseShort(value));
    }

    public Byte getByte(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Byte.valueOf(Byte.parseByte(value));
    }

    public Long getLong(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Long.valueOf(Long.parseLong(value));
    }

    public Double getDouble(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Double.valueOf(Double.parseDouble(value));
    }

    public Float getFloat(String name) {
        String value = getParam(name);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return Float.valueOf(Float.parseFloat(value));
    }

    public Object[] getParamArray(String name) {
        return this.params.get(name);
    }

    public Node getRemote() {
        return this.remote;
    }

    public RequestLine getRequestLine() {
        return this.requestLine;
    }

    public String logstr() {
        String str = "\r\n请求ID_" + getId() + "\r\n" + getHeaderString();
        if (getBodyString() != null) {
            return str + getBodyString();
        }
        return str;
    }

    public void parseCookie(HttpConfig httpConfig2) {
        String cookieline = this.headers.get("cookie");
        if (StrUtil.isNotBlank(cookieline)) {
            this.cookies = new ArrayList();
            this.cookieMap = new HashMap();
            Set<Map.Entry<String, String>> set = Cookie.getEqualMap(cookieline).entrySet();
            ArrayList arrayList = new ArrayList();
            for (Map.Entry<String, String> cookieMapEntry : set) {
                HashMap<String, String> cookieOneMap = new HashMap<>();
                cookieOneMap.put(cookieMapEntry.getKey(), cookieMapEntry.getValue());
                arrayList.add(cookieOneMap);
                Cookie cookie = Cookie.buildCookie(cookieOneMap, httpConfig2);
                this.cookies.add(cookie);
                this.cookieMap.put(cookie.getName(), cookie);
            }
        }
    }

    public void setBodyFormat(HttpConst.RequestBodyFormat bodyFormat2) {
        this.bodyFormat = bodyFormat2;
    }

    public void setBodyString(String bodyString2) {
        this.bodyString = bodyString2;
    }

    public void setChannelContext(ChannelContext channelContext2) {
        this.channelContext = channelContext2;
    }

    public void setCharset(String charset2) {
        this.charset = charset2;
    }

    public void setContentLength(int contentLength2) {
        this.contentLength = contentLength2;
    }

    public void setCookieMap(Map<String, Cookie> cookieMap2) {
        this.cookieMap = cookieMap2;
    }

    public void setCookies(List<Cookie> cookies2) {
        this.cookies = cookies2;
    }

    public void setHeaders(Map<String, String> headers2) {
        this.headers = headers2;
        if (headers2 != null) {
            parseCookie(this.httpConfig);
        }
    }

    public void setHttpConfig(HttpConfig httpConfig2) {
        this.httpConfig = httpConfig2;
    }

    public void setHttpSession(HttpSession httpSession2) {
        this.httpSession = httpSession2;
    }

    public void setIsAjax(Boolean isAjax2) {
        this.isAjax = isAjax2;
    }

    public void setIsSupportGzip(Boolean isSupportGzip2) {
        this.isSupportGzip = isSupportGzip2;
    }

    public void setParams(Map<String, Object[]> params2) {
        this.params = params2;
    }

    public void setRemote(Node remote2) {
        this.remote = remote2;
    }

    public void setRequestLine(RequestLine requestLine2) {
        this.requestLine = requestLine2;
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(long createTime2) {
        this.createTime = createTime2;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.requestLine.toString()).append("\r\n");
        if (getHeaderString() != null) {
            sb.append(getHeaderString()).append("\r\n");
        }
        if (getBodyString() != null) {
            sb.append(getBodyString());
        }
        return sb.toString();
    }

    public boolean isClosed() {
        return this.closed;
    }

    public void setClosed(boolean closed2) {
        this.closed = closed2;
    }

    public String getConnection() {
        return this.connection;
    }

    public void setConnection(String connection2) {
        this.connection = connection2;
    }

    public String getReferer() {
        return getHeader("referer");
    }

    public boolean isNeedForward() {
        return this.needForward;
    }

    public void setNeedForward(boolean needForward2) {
        this.needForward = needForward2;
    }

    public boolean isForward() {
        return this.isForward;
    }

    public void setForward(boolean isForward2) {
        this.isForward = isForward2;
    }
}
